﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 时间跟踪器
    /// </summary>
    public class TimeTracker
    {
        #region Constructors

        public TimeTracker()
        {
            firstTime = lastTime = DateTime.Now; 
        }
         
        private DateTime firstTime;
        private DateTime lastTime;
        #endregion

        /// <summary>
        /// 重置时间计数器，从当前时间点开始计时。
        /// </summary>
        public void Reset()
        {
            lastTime = firstTime = DateTime.Now;
        }

        /// <summary>
        /// 更新时间计数 
        /// </summary>
        /// <returns></returns>
        public double Update()
        {
            var currentTime = DateTime.Now;

            var diffTime = currentTime - lastTime;
            lastTime = currentTime;
            DeltaSeconds = diffTime.TotalSeconds;
            //if (diffTime >= Interval)
            //{
            //    DeltaSeconds = diffTime.TotalSeconds;
            //    lastTime = currentTime;
            //    TimerFired?.Invoke(this, new TimerFiredEventArgs(diffTime));
            //}

            return DeltaSeconds;
        }
        #region Properties 
        /// <summary>
        /// 获取已持续的时间
        /// </summary>
        public TimeSpan ElapsedTime
        {
            get
            {
                return DateTime.Now - firstTime;
            }
        }
        /// <summary>
        /// 时间变化量，计为秒数（相对于上一帧）
        /// </summary>
        public double DeltaSeconds { get; private set; }

        #endregion
    }

    public class TimerFiredEventArgs : EventArgs
    {
        public TimerFiredEventArgs(TimeSpan delta)
        {
            this.Delta = delta;
        }
        public TimeSpan Delta { get; set; }
    }
}
